from django.shortcuts import render
from rest_framework.views import APIView
from rest_framework.response import Response
from .models import *
from .serializers import *
import re
# Create your views here.

class Login(APIView):
    def post(self,request):
        name = request.data['name']
        print(name)
        password = request.data['password']
        user = User.objects.filter(name=name,password=password)
        if user:
            return Response({
                'code':200,
                'message':'登录成功'
            })

class Dings(APIView):
    def post(self,request):
        name = request.data['name']
        tel = request.data['tel']
        gid = request.data['gid']
        uid = request.data['uid']
        try:
            a = re.match(r'^1[3-9]\d{9}$',tel).group()
            print(tel)
            user = User.objects.filter(name=name).first()
            user.tel = tel
            user.save()
            Ding.objects.create(
                uid_id=uid,
                gid_id=gid
            )
            return Response({
                'code':200,
                'message':'添加成功'
            })
        except:
            return Response({
                'code': 405,
                'message': '手机号不符合规范'
            })

class Show(APIView):
    def post(self, request):
        uid = request.data['uid_id']
        ding = Ding.objects.filter(uid=uid)
        dinglist = Dingser(ding, many=True)

        return Response({
            'dinglist': dinglist.data,
        })


class Goods(APIView):
    def post(self,request):
        name = request.data['name']

        good = Good.objects.all()
        goods = Goodser(good, many=True)
        user = User.objects.filter(name=name).first()
        users = Userser(user)

        return Response({
            'good': goods.data,
            'users': users.data,
        })




